<?php
//$Id: global.php 9 2009-04-26 06:35:12Z bole.Chen $
uses('controller');

class globalController extends Controller {

    function __construct() {
        parent::__construct();
    }

    function blank() {
    }

    //网站首页
    function index() {
        $this->debugmsg('globalController::index');

        uses('cache');
        $data['update'] = $this->request['update'];

        //推荐人
        if ($this->request['camefrom']) {
            $user =& getController('users');
            if ($tmp = $user->byName($this->request['camefrom'])) {
                //推荐人保持10年
                if ($tmp['id']) setcookie('camefrom', $tmp['id'], time()+3600*24*365*10, '/');
            }
        }

        //博客新文章
        $data['limit']      = 3;
        $data['sort']       = 'ID';
        $data['sortorder']  = 'DESC';

        if ( (!$result['news'] = cache_get('globalIndexBlog')) || $data['update'] ) {
            $result['news'] = $this->model->indexBlog($data);
            cache_set('globalIndexBlog', $result['news']);
        }

        //新品
        $items =& getController('items') ;
        $tmp = $items->whatnew(18);
        $result['whatnew'] = $tmp['list']['list'];

        $tmp = $items->whathot(18);
        $result['whathot'] = $tmp['list']['list'];

        $tmp = $items->whatoffer(18);
        $result['whatoffer'] = $tmp['list']['list'];

        //热销
        if ( (!$result['topsale'] = cache_get('globalIndexTopSale')) || $data['update'] ) {
            $stat =& getController('stat');
            $stat->request['time'] = date('Y-m-d', time()-3600*24*30);
            //只显示出售中的物品
            $stat->request['issale'] = true;
            $stat->request['flush'] = true;
            $tmp = $stat->top10();

            //30天数据不够 10 个就展示过去一年的
            if ($tmp['topsale']['count'] < 10) {
                $stat->request['time'] = date('Y-m-d', time()-3600*24*365);
                $tmp = $stat->top10();
            }

            //还没有数据？晕，那就先展示热门物品吧！
            if ($tmp['topsale']['count'] < 10) {
                foreach ($result['whathot'] as $v) {
                    $v['iid']   = $v['id'];
                    $v['iname'] = $v['name'];
                    $tmp['topsale']['result'][] = $v;
                }
            }

            $result['topsale'] = $tmp['topsale']['result'];
            cache_set('globalIndexTopSale', $result['topsale']);
        }


        return $result;
    }

    function events() {
    }

    function aboutus() {
    }

    //隐私声明
    function privacy() {
    }

    function feedback() {
        return $this->contactform();
    }

    /*
     * 1 成功 2 失败 3 信息不完整
     */
    function contactform() {
        //姓名、公司名、联系电话、email、其它联系方式、公司说明、可以提供的商品
        $data['type']       =   intval($this->request['type']);
        $data['name']       =   trim($this->request['name']);
        $data['company']    =   trim($this->request['company']);
        $data['tel']        =   trim($this->request['tel']);
        $data['email']      =   trim($this->request['email']);
        $data['im']         =   trim($this->request['im']);
        $data['desc']       =   trim($this->request['desc']);
        $data['product']    =   trim($this->request['product']);

        if ($data['type']) {
            if ($data['name'] && $data['desc']) {
                $result['code'] = ($this->model->contactMail($data) ? 1 : 2);
            } else {
                $result['code'] = 4;
            }
        }

        $result['data'] = $data;
        return $result;
    }

    function ajaxContactForm($v) {
        parse_str($v, $this->request);
        $this->request = array_stripslashes($this->request);

        $result = $this->contactform();
        $this->ajax->quickresult($result['code']);
    }

    //订单状态
    function flow() {
        if ($this->request['iid'] && $this->request['zipcode']) {
            $indent = &getController('indent');

            $v['id'] = $this->request['iid'];
            $rs = $indent->model->indentsList($v);

            $data = $rs['list'][0];

            if ($rs['list'][0]['zipcode'] == $this->request['zipcode']) {
                $data['code'] = 1;
            } else {
                $data['code'] = 2;
            }

            $data['iid'] = $this->request['iid'];
            $data['zipcode'] = $this->request['zipcode'];
        }

        $result['data'] = $data;
        return $result;
    }

    function faq() {
    }

    function copyright() {
    }

    //支持者
    function support() {
    }

    //google sitemaps 站点地图
    function sitemaps() {
        $ic =& getController('items');

        $ic->request['sort']      = 'created';
        $ic->request['sortorder'] = 'desc';
        $ic->pagesize = 200;

        $data['tags']       = $ic->tagsAllList();
        $data['items']      = $ic->itemsList();
        $data['itemslist']  = $ic->catsList();

        return $data;
    }

    function _404() {
        $url = $_SERVER['REQUEST_URI'];
        if ($tmp = split("[/\?]", $url)) {
            array_shift($tmp);
            $_SERVER['PATH_INFO'] = join("/", $tmp);

            if (!$_SERVER['REDIRECT_QUERY_STRING']) {
                $_SERVER['REDIRECT_QUERY_STRING'] = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'],'?')+1);
            }
            parse_str($_SERVER['REDIRECT_QUERY_STRING'], $_req);
            parse_str($_SERVER['REDIRECT_QUERY_STRING'], $_get);

            $_REQUEST   = array_merge($_REQUEST, $_req);
            $_GET       = array_merge($_GET, $_get);

            /*
            if ($this->session['admin']) {
                print_r($tmp);
                print_r($_SERVER['PATH_INFO']);
                print_r($_GET);
                exit;
            }
            */
        }

        $rewrite[] = array('{^/F/}i', 'index.php'); //http://www.loopo.cn/F/avenger 推荐来源
        $rewrite[] = array('{^/index}i', 'index.php');
        $rewrite[] = array('{^/test}i', '/test/test.php');
        $rewrite[] = array('{^/brand}i', 'brand.php');

        $rewrite[] = array('{^/whats?new}i', 'whatnew.php');
        $rewrite[] = array('{^/whats?hot}i', 'whathot.php');
        $rewrite[] = array('{^/whats?miss}i', 'whatmiss.php');
        $rewrite[] = array('{^/whats?off}i', 'whatoffer.php');

        $rewrite[] = array('{^/items?list}i', 'itemslist.php');
        $rewrite[] = array('{^/items?}i', 'items.php');
        $rewrite[] = array('|^/tag/?|i', 'tag.php');
        $rewrite[] = array('|^/for\//?|i', 'tag.php');
        $rewrite[] = array('|^/shopcar/?|i', 'shopcar.php');
        $rewrite[] = array('|^/alertme/?|i', 'alertme.php');
        $rewrite[] = array('|^/addtocar/?|i', 'addtocar.php');
        $rewrite[] = array('|^/checkout/?|i', '/checkout.php');

        $rewrite[] = array('|^/comments/?|i', 'comments.php');
        $rewrite[] = array('|^/wishlist/?|i', 'wishlist.php');
        $rewrite[] = array('|^/trackback/?|i', 'trackback.php');

        $rewrite[] = array('|^/forum/?|i', 'forum.php');
        $rewrite[] = array('|^/faq/?|i', 'faq.php');

        $rewrite[] = array('|^/cats/?|i', 'cats.php');
        $rewrite[] = array('|^/support/?|i', 'support.php');
        $rewrite[] = array('|^/login|i', 'login.php');
        $rewrite[] = array('|^/openid|i', 'openid.php');
        $rewrite[] = array('|^/imnew/?|i', 'imnew.php');
        $rewrite[] = array('|^/flow/?|i', 'flow.php');
        $rewrite[] = array('|^/aboutus/?|i', 'aboutus.php');
        $rewrite[] = array('|^/copyright/?|i', 'copyright.php');
        $rewrite[] = array('|^/privacy/?|i', 'privacy.php');
        $rewrite[] = array('|^/forgotpwd/?|i', 'forgotpwd.php');

        $rewrite[] = array('|^/api/?|i', 'api.php');
        $rewrite[] = array('|^/search/?|i', 'search.php');
        $rewrite[] = array('|^/photo/?|i', 'photo.php');
        $rewrite[] = array('|^/gift/?|i', 'gift.php');
        $rewrite[] = array('|^/feedback/?|i', 'feedback.php');

        foreach ($rewrite as $v) {
            if (preg_match($v[0], $url)) {
                status_header(200);
                include ROOT.$v[1];exit;
            }
        }
    }

    function get_option($setting) {
        $value = wp_cache_get($setting, 'options');

        if ( false === $value ) {
            if ( defined('WP_INSTALLING') )
                $wpdb->hide_errors();
            $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1");
            if ( defined('WP_INSTALLING') )
                $wpdb->show_errors();

            if( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
                $value = $row->option_value;
                wp_cache_set($setting, $value, 'options');
            } else {
                return false;
            }
        }

        return do_filters( 'option_' . $setting, maybe_unserialize($value) );
    }

    function add_option($name, $value = '', $description = '', $autoload = 'yes') {
        global $wpdb;

        // Make sure the option doesn't already exist
        if ( false !== get_option($name) )
            return;

        if ( is_array($value) || is_object($value) )
            $value = serialize($value);

        wp_cache_set($name, $value, 'options');

        $name = $wpdb->escape($name);
        $value = $wpdb->escape($value);
        $description = $wpdb->escape($description);
        $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");

        return;
    }

    function update_option($option_name, $newvalue) {
        global $wpdb;

        if ( is_string($newvalue) )
            $newvalue = trim($newvalue);

        // If the new and old values are the same, no need to update.
        $oldvalue = get_option($option_name);
        if ( $newvalue == $oldvalue ) {
            return false;
        }

        if ( false === $oldvalue ) {
            add_option($option_name, $newvalue);
            return true;
        }

        if ( is_array($newvalue) || is_object($newvalue) )
            $newvalue = serialize($newvalue);

        wp_cache_set($option_name, $newvalue, 'options');

        $newvalue = $wpdb->escape($newvalue);
        $option_name = $wpdb->escape($option_name);
        $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
        if ( $wpdb->rows_affected == 1 ) {
            do_action("update_option_{$option_name}", $oldvalue, $newvalue);
            return true;
        }
        return false;
    }

    function delete_option($name) {
        global $wpdb;
        // Get the ID, if no ID then return
        $option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'");
        if ( !$option_id ) return false;
        $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
        wp_cache_delete($name, 'options');
        return true;
    }

}
?>
